﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：AddOrEditModelPocoViewModel
// 创 建 者：杨程
// 创建时间：2021/10/20 14:26:42
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using V.CodeGenerator.CreateCodes.Model;
using Vampirewal.Core;
using Vampirewal.Core.Attributes;
using Vampirewal.Core.Interface;
using Vampirewal.Core.Models;
using Vampirewal.Core.IoC;
using Vampirewal.Core.SimpleMVVM;
using V.CodeGenerator.Model;

namespace V.CodeGenerator.ViewModel
{
    [VampirewalIoCRegister(ViewModelKey.AddOrEditModelPocoViewModel, RegisterType.ViewModel)]
    public class AddOrEditModelPocoViewModel:BillVM<ModelPoco>
    {
        private IDialogMessage Dialog { get; set; }
        public AddOrEditModelPocoViewModel(IDataContext dc,IDialogMessage dialog):base(dc)
        {
            //构造函数
            Dialog = dialog;

            list= EnumHelper.EnumToList<InheritanceType>().Where(e => e.Classification >= 1 && e.Classification <= 3).OrderBy(e => e.Classification).ToList();
        }

        

        public override void InitData()
        {
            
        }

        private bool IsCreate { get; set; } = false;
        private bool IsUpdate { get; set; } = false;
        public override object GetResult()
        {
            if (IsCreate||IsUpdate)
            {
                return Entity;
            }
            return null;
        }

        public override void PassData(object obj)
        {
            if (obj!=null)
            {
                var current = obj as List<SimplePassData>;

                if (current!=null)
                {
                    projectPoco = current.Find(f => f.Key == "Project")?.Value as ProjectPoco;
                    moduleFunctionPoco = current.Find(f => f.Key == "ModuleFunction")?.Value as ModulePoco;

                    ModelPoco model = current.FirstOrDefault(f => f.Key == "ModelPoco")?.Value as ModelPoco;
                    if (model!=null)
                    {
                        SetEntity(model);
                        Title = $"修改<{Entity.ClassName}>模型";
                        IsUpdate = true;
                        //Entity.ProjectPocoId = projectPoco.ID;
                        //Entity.ClassName = moduleFunctionPoco.PocoName;
                        //Entity.ModuleId = moduleFunctionPoco.ParentId;
                        //Entity.FunctionId = moduleFunctionPoco.ID;
                    }
                    else
                    {
                        Title = $"新建模型";

                        Entity.ProjectPocoId = projectPoco.BillId;
                        Entity.ClassName = moduleFunctionPoco.PocoName;
                        //Entity.ModuleId = moduleFunctionPoco.ParentId;
                        Entity.FunctionId = moduleFunctionPoco.BillId;
                    }
                }
            }
            
        }

        #region 属性
        private ProjectPoco projectPoco { get; set; }

        private ModulePoco moduleFunctionPoco { get; set; }

        public List<EnumberCreditType> list { get; set; }
        #endregion

        #region 公共方法

        #endregion

        #region 私有方法

        #endregion

        #region 命令
        public RelayCommand SaveCommand => new RelayCommand(() => 
        {
            if (string.IsNullOrEmpty( Entity.ClassName))
            {
                Dialog.ShowPopupWindow("类名不能为空！",(Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Error);
                return;
            }

            if (string.IsNullOrEmpty(Entity.DisplayName))
            {
                Dialog.ShowPopupWindow("别名不能为空！", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Error);
                return;

            }

            if (string.IsNullOrEmpty(Entity.TableName))
            {
                Dialog.ShowPopupWindow("数据库名不能为空！", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Error);
                return;

            }

            if (string.IsNullOrEmpty(Entity.InheritanceBaseModel))
            {
                Dialog.ShowPopupWindow("继承基类不能为空！", (Window)View, Vampirewal.Core.WpfTheme.WindowStyle.MessageType.Error);
                return;

            }


            if (!IsUpdate)
            {
                DoAdd();
                IsCreate = true;
                Entity.CreateBy = "admin";
                Entity.CreateTime = DateTime.Now;
            }
            else
            {
                DoUpdate();
            }
            

            

            ((Window)View).Close();
        });


        public RelayCommand<EnumberCreditType> SelectionChangedCommand => new RelayCommand<EnumberCreditType>((s) => 
        {
            Entity.InheritanceBaseModel = s.Key;
        });
        #endregion

        public enum InheritanceType
        {
            [SetClassification(Type = 1)]
            [Display(Name = "TopBaseModel")]
            TopBaseModel=0,
            [SetClassification(Type = 2)]
            [Display(Name = "BaseModel")]
            BaseModel =1,
            [SetClassification(Type = 3)]
            [Display(Name = "TreeBaseModel")]
            TreeBaseModel =2
        }
    }

    
}
